package cn.topevery.garden.wapi.controller;

import hw.topevery.basis.dal.logic.SysAppLogic;
import hw.topevery.basis.dal.logic.SysNamespaceLogic;
import hw.topevery.basis.entity.dto.SysNSTree;
import hw.topevery.basis.entity.po.SysApp;
import hw.topevery.basis.entity.po.SysNamespace;
import hw.topevery.framework.SystemConst;
import hw.topevery.framework.TreeNode;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 *  Controller
 *
 * @author MyBatisGenerator
 * @date 2020-08-12
 */
@Api(tags = "命名空间")
@RestController
@RequestMapping("/sysNs" )
@Slf4j
public class SysNsController extends BaseController {

    @Autowired
    private SysNamespaceLogic sysNamespaceLogic;

    @Autowired
    private SysAppLogic sysAppLogic;

    @ApiOperation("获取类型树形")
    @GetMapping("/tree")
    public Object getNSTree(){
        JsonResult<List<TreeNode>> result = new JsonResult<>(true);
        try {
            List<SysNSTree> menus = sysNamespaceLogic.getNsTree();
            List<TreeNode> nsTree = new ArrayList<>();

            List<SysNSTree> rootMenus = findSysNS(menus, SystemConst.UUID_EMPTY_STRING);

            for (SysNSTree m : rootMenus) {
                TreeNode node = new TreeNode();
                node.setId(m.getNSId());
                node.setParentId(m.getParentId());
                node.setLabel(m.getName());
                node.setValue(m.getNSId());
                node.setChildren(new ArrayList<>());
                buildChildrenNS(menus, node);
                nsTree.add(node);
            }

            result.data = nsTree;
        } catch (Exception ignored) {
            result.setCode(400);
            result.success = false;
            result.msg = ignored.getMessage();
            log.error(ignored.getMessage(), ignored);
        }
        return result;

    }

    @GetMapping("/nsList")
    public Object getNamespaceList() {
        JsonResult<List<SysNamespace>> result = new JsonResult<>();
        try {
            List<SysNamespace> dictList = sysNamespaceLogic.getListNamespace();
            result.data = dictList;
        } catch (Exception ignored) {
            result.setCode(400);
            result.success = false;
            result.msg = ignored.getMessage();
            log.error(ignored.getMessage(), ignored);
        }
        return result;
    }

//    @PostMapping("/addOrEditNS")
//    public JsonResult<Boolean> addOrEditNS(@RequestBody SysNamespace nsEntity) {
//        log.info("addOrEditNS"+ nsEntity);
//        System.out.println("addOrEditNS"+ nsEntity);
//        SysUser oUser = UserRuntime.getSysUser();
//        JsonResult<Boolean> result = new JsonResult<>();
//        try {
//            if (oUser != null){
//                SysNamespace entity = null;
//                if (nsEntity.getNsId().isEmpty()){
//                    entity = new SysNamespace();
//                    entity.setNsId(UUID.randomUUID().toString());
//                    entity.setCreateId(oUser.getUserId());
//                    entity.setCreateTime(LocalDateTime.now());
//                }else
//                {
//                    entity = sysNamespaceLogic.getByGuid(nsEntity.getNsId());
//                    entity.setUpdateId(oUser.getUserId());
//                    entity.setUpdateTime(LocalDateTime.now());
//                }
//                entity.setNsName(nsEntity.getNsName());
//                entity.setAppId(nsEntity.getAppId());
//                entity.setRemark(nsEntity.getRemark());
//                entity.setOrderNum(nsEntity.getOrderNum());
//                result.data = sysNamespaceLogic.saveOrUpdate(entity);
//            }
//        } catch (Exception ignored) {
//            result.setCode(400);
//            result.success = false;
//            result.msg = ignored.getMessage();
//            log.error(ignored.getMessage(), ignored);
//        }
//        return result;
//    }

    private void buildChildrenNS(List<SysNSTree> dataSource, TreeNode parentNode) {
        List<SysNSTree> nodes = findSysNS(dataSource, parentNode.getId());
        for (SysNSTree m : nodes) {
            TreeNode node = new TreeNode();
            node.setId(m.getNSId());
            node.setParentId(m.getParentId());
            node.setLabel(m.getName());
            node.setValue(m.getNSId());
            node.setChildren(new ArrayList<>());
            buildChildrenNS(dataSource, node);
            parentNode.getChildren().add(node);
        }
    }

    private List<SysNSTree> findSysNS(List<SysNSTree> dataSource, String menuId) {
        List<SysNSTree> newMenuList = new ArrayList<>();
        for (SysNSTree menu : dataSource) {
            if (menu.getParentId().equals(menuId)) {
                newMenuList.add(menu);
            }
        }
        return newMenuList;
    }

    @GetMapping("/allAppList")
    public JsonResult getAllAppList() {
        JsonResult<List<SysApp>> res = new JsonResult<>(true);
        try {
            List<SysApp> allList =sysAppLogic.getAllAppList();
            res.data = allList;
        } catch (Exception e) {
            res.setCode(400);
            res.success = false;
            res.msg = e.getMessage();
            log.error(e.getMessage(), e);
        }
        return res;
    }

}
